from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods


# 获取订单页面的所有数据
class OrderViewSetSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderInfo
        fields = "__all__"


# 为了重写sku,定义序列化器，返回前端所要的sku中包含的两个字段
class SKUModerlSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ("name","default_image_url")

# 为了重写skus，定义序列化器，因为前端中，skus包含了三个字段，且字段sku更包含子层，所以sku也要重写
class OrderGoodsModelSerializer(serializers.ModelSerializer):
    # 2,重写sku
    sku = SKUModerlSerializer(read_only=True) # 导入序列化器赋值给sku，sku就拥有了前端所需要的字段
    class Meta:
        model = OrderGoods
        fields = ("sku","price","count") # 此时，两个序列化器加持，skus,就完整了

# 获取订单详情页面下面的商品信息
class OrdersModelSerializer(serializers.ModelSerializer):

    #1,重写skus,实际上就是Ordergoods
    skus = OrderGoodsModelSerializer(read_only=True,many=True) #一方，写many。

    class Meta:
        model = OrderInfo
        # fields = ("order_id","status","create_time",
        #           "user","total_count","total_amount",
        #           "freight","pay_method","skus")
        fields = "__all__" # 这里面所有的字段，都是前端所需，因为skus被前端取出两层，所以才定义了两个序列化器，实现skus数据的拼组


